package com.kylin.system.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kylin.common.core.exception.ServiceException;
import com.kylin.common.core.service.impl.DataServiceImpl;
import com.kylin.system.common.utils.DictUtils;
import com.kylin.system.entity.SysDictData;
import com.kylin.system.entity.SysDictType;
import com.kylin.system.mapper.SysDictDataMapper;
import com.kylin.system.mapper.SysDictTypeMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 字典 业务层处理
 * 
 * @author wuhao
 */
@Service
public class SysDictTypeService extends DataServiceImpl<SysDictTypeMapper, SysDictType>
{

    /** 字典类型字段值 */
    private static final String DICT_TYPE_FIELD = "dict_type";

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
//    @PostConstruct
//    public void init()
//    {
//        List<SysDictType> dictTypeList = mapper.selectList(null);
//        for (SysDictType dictType : dictTypeList)
//        {
//            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
//            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
//        }
//    }

    /**
     * 根据字典类型查询字典数据
     * 
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    public List<SysDictData> selectDictDataByType(String dictType)
    {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (ObjectUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (ObjectUtils.isNotEmpty(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return Collections.emptyList();
    }

    /**
     * 批量删除字典类型信息
     * 
     * @param dictIds 需要删除的字典ID
     * @return 结果
     */
    public int deleteDictTypeByIds(Long[] dictIds)
    {
        for (Long dictId : dictIds)
        {
            SysDictType dictType = getById(dictId);
            SysDictData where = new SysDictData();
            where.getQueryWrapper().eq(DICT_TYPE_FIELD, dictType.getDictType());
            long count = dictDataMapper.selectCount(where.getQueryWrapper());
            if (count > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
        }
        int count = mapper.deleteBatchIds(Arrays.asList(dictIds));
        if (count > 0)
        {
            DictUtils.clearDictCache();
        }
        return count;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache()
    {
        DictUtils.clearDictCache();
    }

    /**
     * 新增保存字典类型信息
     * 
     * @param dictType 字典类型信息
     * @return 结果
     */
    public int insertDictType(SysDictType dictType)
    {
        dictType.perInsert();
        int row = mapper.insert(dictType);
        if (row > 0)
        {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 修改保存字典类型信息
     * 
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateDictType(SysDictType dictType)
    {
        SysDictType oldDict = getById(dictType.getId());

        UpdateWrapper<SysDictData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(DICT_TYPE_FIELD,oldDict.getDictType() )
                .set(DICT_TYPE_FIELD, dictType.getDictType());
        dictDataMapper.update(null, updateWrapper);
        int row = mapper.updateById(dictType);
        if (row > 0)
        {
            DictUtils.clearDictCache();
        }
        return row;
    }
}
